package com.nuotu.datawall.clouddata.layoutData;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.AbsListView;
import android.widget.ListView;

import com.google.gson.reflect.TypeToken;
import com.nuotu.datawall.clouddata.adapter.ReaderCount_adapter;
import com.nuotu.datawall.clouddata.been.ReaderCount;
import com.nuotu.datawall.clouddata.been.RtInfo;
import com.nuotu.datawall.clouddata.control.Token;
import com.nuotu.datawall.clouddata.util.JsonUtil;
import com.nuotu.datawall.clouddata.util.ServerConfig;
import com.nuotu.datawall.clouddata.util.Tool;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class ReaderCountLayout  {
    private static volatile ReaderCountLayout instance;
    private static Context mContext;
    private ListView readerList;
    private ReaderCount_adapter readerCount_adapter;
    private List<ReaderCount> readerCountList =new ArrayList<ReaderCount>();
    public static ReaderCountLayout getInstance(Context context) {
        if (instance == null) {
            synchronized (ReaderCountLayout.class) {
                if (instance == null) {
                    mContext=context;
                    instance = new ReaderCountLayout();
                }
            }
        }
        return  instance;
    }
    public ReaderCountLayout init(ListView listView){
        this.readerList=listView;
        readerCount_adapter=new ReaderCount_adapter(mContext,readerCountList);
        readerList.setAdapter(readerCount_adapter);
        return instance;
    }
    public void refreshData(){
        RequestParams params = Tool.getRequestParams(mContext, ServerConfig.POST_READERCOUNT_TOP10_URL_KEY);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                final RtInfo rtInfo= JsonUtil.fromJson(result, RtInfo.class);
                 List<ReaderCount> readerCountl=JsonUtil.fromArrayJson(JsonUtil.toJson(rtInfo.getData()),new TypeToken<List<ReaderCount>>() {}.getType());
                readerCountList.clear();
                readerCountList.addAll(readerCountl);
                ReaderCount readerCount =new ReaderCount();
                readerCount.setNum(-1);
                readerCount.setXh(-1);
                readerCount.setDepartmentName("一一一一一一一一");
                readerCount.setUserName("一一一一一一一一");
                readerCountList.add(readerCount);
                readerCount_adapter.notifyDataSetChanged();
//                x.task().autoPost(new Runnable() {
//                    @Override
//                    public void run() {
//                        echartView.refreshEchartsWithOption(EPieOptionUtil.getPieOptions(listMap));
//                    }
//                });
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Token.getInstance(mContext).refreshToken(ex.toString());
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });

    }

}
